This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.
im <- readImage("papSmear.jpg")
dim(im)
## [1] 493 335 3
plot(im) # raster method means within R
# reshape image into a data frame
df = data.frame(
red = matrix(im[,,1], ncol=1),
green = matrix(im[,,2], ncol=1),
blue = matrix(im[,,3], ncol=1)
)
str(df)
## 'data.frame': 165155 obs. of 3 variables:
## $ red : num 0.886 0.906 0.933 0.953 0.957 ...
## $ green: num 0.694 0.714 0.741 0.761 0.765 ...
## $ blue : num 0.71 0.729 0.757 0.776 0.78 ...
### compute the k-means clustering
K = kmeans(df,5)
str(K)
## List of 9
## $ cluster : int [1:165155] 3 3 4 4 4 4 4 4 4 4 ...
## $ centers : num [1:5, 1:3] 0.707 0.502 0.885 0.966 0.61 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : chr [1:5] "1" "2" "3" "4" ...
## .. ..$ : chr [1:3] "red" "green" "blue"
## $ totss : num 8016
## $ withinss : num [1:5] 99.8 77.6 167 162.5 111.5
## $ tot.withinss: num 618
## $ betweenss : num 7398
## $ size : int [1:5] 19219 5534 18104 105805 16493
## $ iter : int 6
## $ ifault : int 0
## - attr(*, "class")= chr "kmeans"
head(K$centers)
## red green blue
## 1 0.7068195 0.6314813 0.6412725
## 2 0.5015547 0.3339265 0.4417115
## 3 0.8852673 0.6515596 0.7181804
## 4 0.9664161 0.8012112 0.8026724
## 5 0.6097807 0.5254588 0.5571255
df$label = K$cluster
head(df)
### Replace the color of each pixel in the image with the mean
### R,G, and B values of the cluster in which the pixel resides:
# get the coloring
colors = data.frame(
label = 1:nrow(K$centers),
R = K$centers[,"red"],
G = K$centers[,"green"],
B = K$centers[,"blue"]
)
dim(colors)
## [1] 5 4
colors
# merge color codes on to df
# IMPORTANT: we must maintain the original order of the df after the merge!
df$order = 1:nrow(df)
df = merge(df, colors)
df = df[order(df$order),]
df$order = NULL
head(df)
# get mean color channel values for each row of the df.
R <- matrix(df$R, nrow=dim(im)[1])
G <- matrix(df$G, nrow=dim(im)[1])
B <- matrix(df$B, nrow=dim(im)[1])
# reconstitute the segmented image in the same shape as the input image
im.segmented <- array(dim=dim(im))
im.segmented[,,1] = R
im.segmented[,,2] = G
im.segmented[,,3] = B
#im.segmented <- rotate(im.segmented, 90)
#im.segmented <- flop(im.segmented )
#dim(im.segmented)
#str(im.segmented)
#class(im.segmented)
#EBImage::writeImage(im.segmented, files = "im.segmented.jpg")
# View the result
#layout(t(1:2))
## convert matrix to Image
im.segmented <- Image(im.segmented, colormode=Color)
class(im.segmented)
## [1] "Image"
## attr(,"package")
## [1] "EBImage"
class(im)
## [1] "Image"
## attr(,"package")
## [1] "EBImage"
plot(im.segmented)
plot(im)
dim(im.segmented)
## [1] 493 335 3
dim(im)
## [1] 493 335 3
get_kmean_image <- function(file, k){
im <- readImage(file)
# reshape image into a data frame
df = data.frame(
red = matrix(im[,,1], ncol=1),
green = matrix(im[,,2], ncol=1),
blue = matrix(im[,,3], ncol=1)
)
### compute the k-means clustering
K = kmeans(df,k)
df$label = K$cluster
### Replace the color of each pixel in the image with the mean
### R,G, and B values of the cluster in which the pixel resides:
# get the coloring
colors = data.frame(
label = 1:nrow(K$centers),
R = K$centers[,"red"],
G = K$centers[,"green"],
B = K$centers[,"blue"]
)
# merge color codes on to df
# IMPORTANT: we must maintain the original order of the df after the merge!
df$order = 1:nrow(df)
df = merge(df, colors)
df = df[order(df$order),]
df$order = NULL
# get mean color channel values for each row of the df.
R <- matrix(df$R, nrow=dim(im)[1])
G <- matrix(df$G, nrow=dim(im)[1])
B <- matrix(df$B, nrow=dim(im)[1])
# reconstitute the segmented image in the same shape as the input image
im.segmented <- array(dim=dim(im))
im.segmented[,,1] = R
im.segmented[,,2] = G
im.segmented[,,3] = B
## convert matrix to Image
im.segmented <- Image(im.segmented, colormode=Color)
par(mfrow=c(1,2))
plot(im.segmented, title= "reconstitute image using k-mean clustering")
text(470, -50, "k-means reconstituted image ", cex = 1.5)
plot(im, title= "original image")
text(500, -50, " Original image", cex = 1.5)
}
get_kmean_image(file = "basales.jpg", k = 6)